package com.webserver.controller;

import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import com.webserver.vo.Article;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ArticleController {
    public static final String ARTICL_DIR = "./articles/";

    static {
        File dir = new File(ARTICL_DIR);
        if (!dir.exists()) { //如果该目录不存在则自动创建
            dir.mkdir();
        }
    }

    /**
     * 处理发表文章操作
     *
     * @param request
     * @param response
     */
    public void writeArticle(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理发表文章...");

        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        System.out.println(title + "," + author + "," + content);

        if (title == null || author == null || content == null) {
            File file = new File("./webapps/myweb/article_fail.html");
            response.setEntity(file);
            return;
        }

        Article article = new Article(title, author, content);
        try (
                FileOutputStream fos = new FileOutputStream(ARTICL_DIR + title + ".obj");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            oos.writeObject(article);
            File file = new File("./webapps/myweb/article_success.html");
            response.setEntity(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("发表文章完毕！");
    }

    public void showAllArticle(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理动态页面");
        //1 将users目录中的所有用户读取回来
        List<Article> articlesList = new ArrayList<>();
        //扫描users目录中的所有.obj文件，并将每个文件中的User对象反序列化回来后存入userList
        File dir = new File(ARTICL_DIR);
        File[] subs = dir.listFiles(f -> f.getName().endsWith(".obj"));
        for (File userFile : subs) {
            try (
                    FileInputStream fis = new FileInputStream(userFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ) {
                Article article = (Article) ois.readObject();
                articlesList.add(article);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println(articlesList);

        //2 将所有用户拼接到html代码中并生成页面
        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>用户列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>文章列表</h1>");
        pw.println("<table border=\"1\">");
        pw.println("<tr>");
        pw.println("<td>标题</td>");
        pw.println("<td>作者</td>");
        pw.println("</tr>");

        for (Article article : articlesList) {
            pw.println("<tr>");
            pw.println("<td><a href=\"./showContent/?title ="+article.getTitle()+"\">"+article.getTitle()+"</a></td>");
            pw.println("<td>" + article.getAuthor() + "</td>");
            pw.println("</tr>");
        }
        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");

        //3 设置响应头Content-Type，告知浏览器这些动态数据是一个页面
        response.setContentType("text/html");


        System.out.println("动态页面处理完毕");
    }

    public void showContent(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理读取文章");
        Article article = new Article();
        List<Article> articleList = new ArrayList<>();
        //扫描users目录中的所有.obj文件，并将每个文件中的User对象反序列化回来后存入userList
        File dir = new File(ARTICL_DIR);
        File[] subs = dir.listFiles(f -> f.getName().endsWith(".obj"));
        for (File userFile : subs) {
            try (
                    FileInputStream fis = new FileInputStream(userFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ) {
                article = (Article) ois.readObject();
                articleList.add(article);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println(article);

        String str = request.getUri().substring(request.getUri().indexOf("=")+1);
        System.out.println(str);

        for (Article article1 : articleList){
            if(article1.getTitle().equals(str)){
                System.out.println(article1);
                article = article1;
            }
        }

        System.out.println(article);
        //2 将所有用户拼接到html代码中并生成页面
        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>文章内容</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>文章内容</h1>");
        pw.println("<table border=\"1\">");
        pw.println("<tr>");
        pw.println("<td>标题</td>");
        pw.println("<td>"+article.getTitle() +"</td>");
        pw.println("</tr>");
        pw.println("<tr>");
        pw.println("<td>作者</td>");
        pw.println("<td>"+article.getAuthor()+"</td>");
        pw.println("</tr>");
        pw.println("<tr>");
        pw.println("<td>内容</td>");
        pw.println("<td>"+article.getContent()+"</td>");
        pw.println("</tr>");
        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");

        //3 设置响应头Content-Type，告知浏览器这些动态数据是一个页面
        response.setContentType("text/html");

        System.out.println("文章读取完毕");
    }
}

